package com.csist.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.csist.pojo.Announcement;
import com.csist.pojo.Events;
import com.csist.service.impl.AnnouncementServiceImpl;
import com.csist.utils.Upload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 *
 * 公告 前端控制器【done】
 *
 *
 * @author wy
 * @since 2021-12-01
 */
@RestController
@RequestMapping("/announcement")
public class AnnouncementController {
    @Autowired
    private AnnouncementServiceImpl announcementService;

    @Value("${myConfig.announcementPageNum}")
    private int pageNum;
    @Value("${myConfig.announcementCoverPath}")
    private String announcementCoverPath;

    /**
     * 通知(封面)图片上传
     * @param file 文件
     * @return "filed":上传失败,上传完成后的文件绝对地址:上传成功
     */
    @RequestMapping("/announcementCoverUpload")
    public String eventsCoverUpload(MultipartFile file){
        return Upload.upload(file,announcementCoverPath);
    }

    /**
     * 公告添加/修改
     * @param announcement
     * @return true: 新增公告成功, false; 新增公告失败
     */
    @RequestMapping("/add")
    public boolean addAnnouncement(@RequestBody Announcement announcement){
        return announcementService.saveOrUpdate(announcement);
    }

    /**
     * 通过id集合删除通知公告
     * @param Ids 通知公告
     * @return true: 删除公告成功, false; 删除公告失败
     */
    @RequestMapping("/remove")
    public boolean removeAnnouncement(List<Integer> Ids){
        return announcementService.removeByIds(Ids);
    }

    /**
     * 根据标题查询通知公告
     * @param title 查询标题
     * @param index 页码(在application.yaml的eventsPageNum中配置一页可加载数据条数)
     * @return 通知公告集合
     */
    @RequestMapping("/getAnnouncementByTitle")
    public List<Events> getAnnouncementByTitle(@RequestParam("search") String title, int index){
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.like("title",title);
        wrapper.orderByDesc("id");
        wrapper.last("limit "+(index*pageNum)+","+pageNum);
        return announcementService.list(wrapper);
    }

    /**
     * 根据查询获取总页数
     * @param title 查询标题
     * @return 页数
     */
    @RequestMapping("/getAnnouncementByTitleSize")
    public int getAnnouncementByTitleSize(@RequestParam("search")String title){
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.like("title",title);
        wrapper.orderByDesc("id");
        int pageSize = announcementService.count(wrapper);
        if (pageSize<pageNum){
            return 1;
        }
        return (int)Math.ceil((double) pageSize/pageNum);
    }

    /**
     * 根据id集合删除通知公告数据
     * @param ids id集合
     * @return true:删除成功,false删除失败
     */
    @RequestMapping("/delete")
    public Boolean deleteEventByIds(@RequestBody List<Integer> ids){
        return announcementService.removeByIds(ids);
    }


    /**
     * 根据活动id获取活动数据
     * @param id 活动id
     * @return 活动数据
     */
    @RequestMapping("/getOne")
    public Announcement getAnnouncementById(int id){
        return announcementService.getById(id);
    }
}
